package cn.foodProject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import cn.foodProject.R;
import cn.foodProject.bean.MyOkhttp;
import cn.foodProject.bean.MyGson;
import cn.foodProject.bean.MyUrl;
import cn.foodProject.gsonBean.CommentDetailsGsonBean;
import cn.foodProject.gsonBean.FoodDetailsGsonBean;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

//评论详细信息
public class CommentDetailsActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "CommentDetailsActivity";
    private TextView tv_food_name;
    private TextView tv_comment;
    private Button btn_NO;
    private Toolbar toolbar_comment_datails;
    private int Id;
    private Context context = this;
    private String food_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment_details);
        initView();
        //获取上一个活动传来的id
        Intent intent = getIntent();
        Id = intent.getIntExtra("id",-1);

        new Thread(){
            @Override
            public void run() {
                super.run();
                updateDetails();
            }
        }.start();
    }

    private void initView() {
        tv_food_name = (TextView) findViewById(R.id.tv_food_name);
        tv_comment = (TextView) findViewById(R.id.tv_comment);
        btn_NO = (Button) findViewById(R.id.btn_NO);

        btn_NO.setOnClickListener(this);
        toolbar_comment_datails = (Toolbar) findViewById(R.id.toolbar_comment_datails);

        //toolbar相关
        setSupportActionBar(toolbar_comment_datails);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar_comment_datails.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_NO:
                finish();
                break;
        }
    }

    //同步更新评论详情的方法
    public void updateDetails() {
        try {
            Response response = MyOkhttp.getResponse(MyUrl.getUrl("/comment/findone/" + Id));
            //当请求成功时
            if (response.isSuccessful()) {
                String s = response.body().string();
                final CommentDetailsGsonBean commentDetailsGsonBean = (CommentDetailsGsonBean) MyGson.fromJson(s, CommentDetailsGsonBean.class);

                if (commentDetailsGsonBean.isSuccess()){
                    //获取当前食品的id
                    int foodId = commentDetailsGsonBean.getData().getFoodid();

                    //根据食品id获取到当前评论的食品名称
                    Response response1 = MyOkhttp.getResponse(MyUrl.getUrl("/food/findone/" + foodId));
                    if (response1.isSuccessful()) {
                        String string  = response1.body().string();
                        FoodDetailsGsonBean foodDetailsGsonBean = (FoodDetailsGsonBean) MyGson.fromJson(string,FoodDetailsGsonBean.class);

                        if(foodDetailsGsonBean.isSuccess()){
                            food_name =  foodDetailsGsonBean.getData().getName();
                        }
                    } else {
                        Log.d(TAG, "onFailure: 请求失败");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(context, "失败!请检查输入或网络!", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    //更新ui
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv_comment.setText(commentDetailsGsonBean.getData().getComment());
                            tv_food_name.setText(food_name);
                        }
                    });
                }else {
                    Log.d(TAG, "updateDetails: "+commentDetailsGsonBean.getMessage());
                }
            } else {
                Log.d(TAG, "onFailure: 请求失败");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "失败!请检查输入或网络!", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
